package com.system.post.web;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.system.common.util.ExtAjaxResponse;
import com.system.common.util.ExtPageable;
import com.system.post.entity.Post;
import com.system.post.entity.dto.PostQueryDTO;
import com.system.post.service.IPostService;

@Controller
@RequestMapping("/post")
public class PostController 
{
	@Autowired
	private IPostService postService;
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Post post) 
	{
		if(post.getPostId()==null) {
			try {
				postService.saveOrUpdate(post);
				return new ExtAjaxResponse(true,"岗位添加成功");
			}catch(Exception e){
				return new ExtAjaxResponse(false,"岗位添加失败");}
		}else{	
			try {		
				postService.saveOrUpdate(post);
				return new ExtAjaxResponse(true,"岗位信息更新成功");
			}catch (Exception e) {
				return new ExtAjaxResponse(false,"岗位信息更新失败");
			}	
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse Delete(@RequestParam Long postId) {
		try {
			Post post = postService.findOne(postId);
			postService.delete(post);
			return new ExtAjaxResponse(true,"删除成功");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败！请先删掉对应员工");
		}

		
	}
	@RequestMapping("/deletePosts")
	public @ResponseBody ExtAjaxResponse Deletes(@RequestParam Long[] ids) {
		
		
		try {
			List<Long> lists = Arrays.asList(ids);
			if (null!=lists) {
				postService.delete(lists);
			}
			return new ExtAjaxResponse(true,"删除成功");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败！请先删掉对应员工信息！");
		}

		
	}
	@RequestMapping("/findOne")
	public @ResponseBody Post findOne(@RequestParam Long id) 
	{
		Post post =  postService.findOne(id);
		
		return post;
	}
	
	
	@RequestMapping("/findAll")
	public @ResponseBody List<Post> findAll() 
	{
		 List<Post> lists =  postService.findAll();
		
		return lists;
	}
	@RequestMapping("/test")
	public @ResponseBody Long test(HttpServletRequest request) {
		HttpSession session = request.getSession();
		return (Long) session.getAttribute("postId");
	}
	@RequestMapping("/findPage")
	public @ResponseBody Page<Post> findPage(PostQueryDTO postQueryDTO,ExtPageable pageable) 
	{
		Page<Post> page =  postService.findAll(PostQueryDTO.getSpecification(postQueryDTO), pageable.getPageable());
		
		return page;
	}
}
